package com.example.service.impl;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.example.service.MailServcie;

/**
 * @ClassName: MailServiceImpl
 * @Description: SpringBoot发送邮件实现类
 * @author liuwj3
 * @date 2019年1月20日 上午10:34:30
 *
 */
@Service
public class MailServiceImpl implements MailServcie {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Value("${spring.mail.username}")
	private String from;
	
	@Autowired
	private JavaMailSender mailSender;

	@Override
	public void sendSimpleMail(String to, String subject, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setTo(to);
		message.setSubject(subject);
		message.setText(content);
		message.setFrom(from);
		mailSender.send(message);
	}
	
	@Override
	public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setTo(to);
		helper.setSubject(subject);
		// true代表是HTML邮件
		helper.setText(content, true);
		helper.setFrom(from);
		mailSender.send(message);
	}

	@Override
	public void sendAttachmentsMail(String to, String subject, String content, String filePath)
			throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		
		FileSystemResource file = new FileSystemResource(new File(filePath));
		helper.addAttachment(file.getFilename(), file);
		// 发送多个附件，多次使用addAttachment即可
		// messageHelper.addAttachment(file.getFilename() + "_test", file);
		mailSender.send(message);
		
	}

	@Override
	public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
		logger.info("发送静态图片邮件开始：{}, {}, {}, {}, {}", to, subject, content, rscPath, rscId);
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(message, true);
			helper.setFrom(from);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);
			
			FileSystemResource res = new FileSystemResource(new File(rscPath));
			helper.addInline(rscId, res);
			// 发送多个图片，重复addInline即可
			// helper.addInline(rscId3, res2);
			
			mailSender.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			logger.error("发送图片邮件失败：" + e.getMessage());
		}
		
		
	}

}
